package com.sfl.api_backend.controller.download;

import com.sfl.api_backend.entity.Imgs;
import com.sfl.api_backend.service.IImgsService;
import com.sfl.api_backend.service.ILabelsService;
import com.sfl.api_backend.utils.Api.RestResult;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author szc
 * @create 2023/4/8 17:10
 */
@ResponseBody
@RequestMapping("/dcm")
@Controller
public class DcmSeries {

    @Resource
    private IImgsService imgsService;

    @Resource
    private ILabelsService labelsService;


    @GetMapping("/getDcm/{imageId}")
    @Async
    public RestResult getDcmSeriesNumber(@PathVariable("imageId") String imageId)  {
        Imgs img = imgsService.getById(imageId);
        String imgPath = img.getViewPath();
        // 遍历文件夹，获取dcm文件数量
        File file = new File(imgPath);
        File[] files = file.listFiles();
        int dcmNumber = 0;
        for (File f : files) {
            if (f.getName().endsWith(".dcm")) {
                dcmNumber++;
            }
        }
        return RestResult.success(dcmNumber);
    }

    @GetMapping("/getImg/{imageId}/{seriesId}")
    @Async
    public ResponseEntity<byte[]> getImg(@PathVariable("imageId") String imageId, @PathVariable("seriesId") String seriesId) throws IOException, ExecutionException, InterruptedException {
        Future<InputStream> _future_inputStream = getDcm(imageId, seriesId, true);
        return ResponseEntity.ok().body(_future_inputStream.get().readAllBytes());
    }

    @GetMapping("/getLabel/{lableId}/{seriesId}")
    @Async
    public ResponseEntity<byte[]> getLabel(@PathVariable("lableId") String imageId, @PathVariable("seriesId") String seriesId) throws IOException, ExecutionException, InterruptedException {
        Future<InputStream> _future_inputStream = getDcm(imageId, seriesId, false);
        return ResponseEntity.ok().body(_future_inputStream.get().readAllBytes());
    }


    @Async
    Future<InputStream> getDcm(String imageId, String seriesID, Boolean isImg) throws FileNotFoundException {

        String imgPath;

        if (isImg) {
            imgPath = imgsService.getById(imageId).getImgPath();
        }
        else{
            imgPath = labelsService.getById(imageId).getLabelPath();
        }

        if (imgPath == null) {
            throw new FileNotFoundException("No such file");
        }

        String SeriesPath = Path.of(imgPath).getParent().toString() + "\\" + seriesID + ".dcm";

        return AsyncResult.forValue(new FileInputStream(SeriesPath));

    }

}
